#include "PuzzleGameData.h"   
#include <time.h> 
#include "PuzzleGuess.h" 
#include "cocos2d.h"



NS_GAME_BEGIN 

PuzzleGameData::PuzzleGameData()
:userDefaultData(nullptr)
,sqliteOP(nullptr)
,dbFile()
{
	userDefaultData = cocos2d::UserDefault::getInstance(); 
	sqliteOP = new SQLiteWrapper();
	this->dbFile = cocos2d::FileUtils::getInstance()->fullPathForFilename(PUZZLEGUESS_DB_FILE); 
	
	#ifdef CC_PLATFORM_ANDROID 

	std::string  destFile = cocos2d::StringUtils::format("%s%s", cocos2d::FileUtils::getInstance()->getWritablePath().c_str(), PUZZLEGUESS_DB_FILE);
	/*if(cocos2d::FileUtils::getInstance()->isFileExist(destFile))
	{
		CCLOG("delete dbFile:%s",destFile.c_str() );
		remove(destFile.c_str());
	}*/

	if(!cocos2d::FileUtils::getInstance()->isFileExist(destFile))
	{
		CCLOG("begin copy the database file");

		CCLOG("get Data From PUZZLEGUESS_DB_FILE");
		cocos2d::Data dbData = cocos2d::FileUtils::getInstance()->getDataFromFile(PUZZLEGUESS_DB_FILE);  
		CCLOG("pData2 size:%d",dbData.getSize());

		FILE *fp = fopen(destFile.c_str(), "wb");
		if(fp)
		{
			CCLOG("read PUZZLEGUESS_DB_FILE success");

			fwrite(dbData.getBytes(),dbData.getSize(),1,fp); 

			CCLOG("write %s success",destFile.c_str());
			fclose(fp);
		} 
		 
		CCLOG("end copy the database file");
	}
	
	this->dbFile = destFile;
		 
	#endif // CC_PLATFORM_ANDROID

	
	CCLOG("puzzleguess game writable path:%s",cocos2d::FileUtils::getInstance()->getWritablePath().c_str());
	CCLOG("dbFile:%s",this->dbFile.c_str());

}


PuzzleGameData::~PuzzleGameData()
{
	if(userDefaultData)
	{
		userDefaultData->destroyInstance();
	}
	CC_SAFE_DELETE(sqliteOP);
}

void PuzzleGameData::init()
{  
	time_t currentTimeStamp;
	struct tm *localCurrentTimeInfo;
	time(&currentTimeStamp);
	localCurrentTimeInfo = localtime(&currentTimeStamp);
	
	this->userDefaultData->setStringForKey("lauchTime", cocos2d::StringUtils::format("%d", currentTimeStamp)  );
	this->userDefaultData->setStringForKey("Time", cocos2d::StringUtils::format("%4d-%02d-%02d %02d:%02d:%02d"
		, localCurrentTimeInfo->tm_year + 1900, localCurrentTimeInfo->tm_mon+1, localCurrentTimeInfo->tm_mday
		, localCurrentTimeInfo->tm_hour, localCurrentTimeInfo->tm_min, localCurrentTimeInfo->tm_sec)  );

	
	db_open(); 

	auto updateStatement = this->sqliteOP->Statement("update PG_Picture set PicturePath=? where Id=?");
	updateStatement->Bind(0, "images/classic/classic_pic0.jpg");
	updateStatement->Bind(1, 6);
	updateStatement->Execute();


	this->sqliteOP->DirectStatement("insert into PG_Picture (PictureTypeId,PicturePath) values (1,\"images/classic/classic_pic5.jpg\")");
}

void PuzzleGameData::db_open()
{
	if(!this->sqliteOP->Open(this->dbFile))
	{
		CCLOGERROR("open db failed:%s",this->sqliteOP->LastError().c_str()); 
	}
}

void PuzzleGameData::db_close()
{
	if(!this->sqliteOP->Close())
	{
		CCLOG("close db failed");
	}
}

void PuzzleGameData::getPicturList(std::vector<PG_Picture*> *vtPicture)
{
	if(!vtPicture)
	{
		return ;
	}

	SQLiteStatement* stmt = this->sqliteOP->Statement("select * from PG_Picture");
	
	while(stmt->NextRow())
	{  
		auto newPicture = PG_Picture::create();
		newPicture->setId(stmt->ValueInt(0));
		newPicture->setPictureTypeId(stmt->ValueInt(1));
		newPicture->setPicturePath(stmt->ValueString(2));

		vtPicture->push_back(newPicture);
	} 
} 

PuzzleGameData::CGarbo PuzzleGameData::GarboSingleton;
PuzzleGameData* PuzzleGameData::sharedPuzzleGameData = nullptr;
PuzzleGameData* PuzzleGameData::getInstance()
{
	if(sharedPuzzleGameData == nullptr)
	{
		sharedPuzzleGameData = new PuzzleGameData();  
	}
	return sharedPuzzleGameData;
}
 

// get value methods

/**
@brief Get bool value by key, if the key doesn't exist, a default value will return.
    You can set the default value, or it is false.
* @js NA
*/
bool PuzzleGameData::getBoolForKey(const char* pKey)
{
	return this->userDefaultData->getBoolForKey(pKey);
}

/**
* @js NA
*/
bool PuzzleGameData::getBoolForKey(const char* pKey, bool defaultValue)
{
	return this->userDefaultData->getBoolForKey(pKey, defaultValue);
}

 /**
@brief Get integer value by key, if the key doesn't exist, a default value will return.
    You can set the default value, or it is 0.
* @js NA
*/
int PuzzleGameData::getIntegerForKey(const char* pKey)
{
	return this->userDefaultData->getIntegerForKey(pKey);
}

/**
* @js NA
*/
int PuzzleGameData::getIntegerForKey(const char* pKey, int defaultValue)
{
	return this->userDefaultData->getIntegerForKey(pKey, defaultValue);
}

/**
@brief Get float value by key, if the key doesn't exist, a default value will return.
    You can set the default value, or it is 0.0f.
* @js NA
*/
float PuzzleGameData::getFloatForKey(const char* pKey)
{
	return this->userDefaultData->getFloatForKey(pKey);
}

/**
* @js NA
*/
float PuzzleGameData::getFloatForKey(const char* pKey, float defaultValue)
{
	return this->userDefaultData->getFloatForKey(pKey, defaultValue);
}
/**
@brief Get double value by key, if the key doesn't exist, a default value will return.
    You can set the default value, or it is 0.0.
* @js NA
*/

double PuzzleGameData::getDoubleForKey(const char* pKey)
{
	return this->userDefaultData->getDoubleForKey(pKey);
}

/**
* @js NA
*/
double PuzzleGameData::getDoubleForKey(const char* pKey, double defaultValue)
{
	return this->userDefaultData->getDoubleForKey(pKey, defaultValue);
}

/**
@brief Get string value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is "".
* @js NA
*/
std::string PuzzleGameData::getStringForKey(const char* pKey)
{
	return this->userDefaultData->getStringForKey(pKey);
}

/**
* @js NA
*/
std::string PuzzleGameData::getStringForKey(const char* pKey, const std::string & defaultValue)
{
	return this->userDefaultData->getStringForKey(pKey, defaultValue);
}

/**
@brief Get binary data value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is null.
* @js NA
* @lua NA
*/
cocos2d::Data PuzzleGameData::getDataForKey(const char* pKey)
{
	return this->userDefaultData->getDataForKey(pKey);
}

/**
* @js NA
* @lua NA
*/
cocos2d::Data PuzzleGameData::getDataForKey(const char* pKey, const cocos2d::Data& defaultValue)
{
	return this->userDefaultData->getDataForKey(pKey, defaultValue);
}
// set value methods

/**
@brief Set bool value by key.
* @js NA
*/
void PuzzleGameData::setBoolForKey(const char* pKey, bool value)
{
	this->userDefaultData->setBoolForKey(pKey, value);
} 

/**
@brief Set integer value by key.
* @js NA
*/
void PuzzleGameData::setIntegerForKey(const char* pKey, int value)
{
	this->userDefaultData->setIntegerForKey(pKey, value);
}

/**
@brief Set float value by key.
* @js NA
*/
void PuzzleGameData::setFloatForKey(const char* pKey, float value)
{
	this->userDefaultData->setFloatForKey(pKey, value);
}

/**
@brief Set double value by key.
* @js NA
*/
void PuzzleGameData::setDoubleForKey(const char* pKey, double value)
{
	this->userDefaultData->setDoubleForKey(pKey, value);
}

/**
@brief Set string value by key.
* @js NA
*/
void PuzzleGameData::setStringForKey(const char* pKey, const std::string & value)
{
	this->userDefaultData->setStringForKey(pKey, value);
}

/**
@brief Set binary data value by key.
* @js NA
* @lua NA
*/
void PuzzleGameData::setDataForKey(const char* pKey, const cocos2d::Data& value)
{
	this->userDefaultData->setDataForKey(pKey, value);
}

/**
@brief Save content to xml file
* @js NA
*/
void PuzzleGameData::flush()
{
	this->userDefaultData->flush();
}

NS_GAME_END